package com.crud.service.impl;

import com.crud.entity.Dept;
import com.crud.mapper.DeptMapper;
import com.crud.service.DeptService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @author hyh
 * @date 2023/06/28 18:44:24
 */
@Service
@Transactional
public class deptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Override
    public int addDept(Dept dept) {
        return deptMapper.insert(dept);
    }

    @Override
    public int deleteDept(Integer id) {
        Dept dept = deptMapper.selectByPrimaryKey(id);
        if (dept.getChildren().size() == 0) {
            return deptMapper.deleteByPrimaryKey(id);
        } else {
            return 2;
        }
    }

    @Override
    public Dept selectByOne(Integer id) {
        return deptMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Dept> findSame(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return deptMapper.selectByExample(null);
    }

    @Override
    public int update(Dept dept) {
        return deptMapper.updateByPrimaryKeySelective(dept);
    }

}
